<script setup>
import MyNav from "../../../components/MyNav.vue";
import MyHead from "../../../components/MyHead.vue";
import MyTable from "../../../components/MyTable.vue";
import MyPager from "../../../components/MyPager.vue";
import { onMounted, reactive, ref } from "vue";
import { deleteApi, deleteBatchApi, pageApi } from "../../../api/axios.js";
import { myPage } from "../../../util/request.js";
import { isNotEmpty, isNotNull } from "../../../util/index.js";
import {notYet} from "../../../util/index.js";
import router from "../../../router/index.js";
// 路径导航
const navItems = [
  { icon: 'User', label: '用户管理' },
  { icon: 'Flag', label: '角色列表' },
];

// 页面头
const headItems = [
  { type: 'ipt', span: 5, placeholder: '按标题搜索', callback: pageLikeTitle },
];

// 表格列
const columns = [
  { label: '角色标题', prop: 'title', width: 120 },
  { label: '角色序号', prop: 'idx', type: 'tag' },
  { label: '角色描述', prop: 'info', type: 'card', tooltip: false },
];

// 表格按钮
const buttons = [
  {label: '菜单列表', callback: roleMenus}
];

// 表格行 + 分页 + 菜单标题
let records = ref([]);
let pageInfo = reactive({ pageNum: 1, pageSize: 5, callback: page });
let title = ref('');

// 分页查询
async function page(pageNum = pageInfo['pageNum'], pageSize = pageInfo['pageSize']) {
  let config = {
    records, pageInfo,
    api: pageApi,
    params: { pageNum, pageSize, pid: 0 },
    args: { module: 'role' },
  };
  if (isNotEmpty(title.value)) config['params']['title'] = title.value;
  await myPage(config);
}

// 按菜单标题模糊查询
function pageLikeTitle(val) {
  if (isNotNull(val) || title.value) {
    title.value = val;
    page();
  }
}

/* ==================== 菜单列表 ==================== */

/**
 * 菜单列表
 *
 * 1. 存储当前角色主键 roleId
 * 2. 存储当前角色标题 roleTitle
 * 3. 路由到 RoleMenus 页面
 *
 * @param row 表格行
 */
function roleMenus(row) {
  sessionStorage.setItem('roleId', row['id']);
  sessionStorage.setItem('roleTitle', row['title'].toString());
  router.push('/RoleMenus')
}

/* ==================== 加载函数 ==================== */


onMounted(() => page());
</script>


<template v-if="records">
  <my-nav :items="navItems"/>
  <my-head :items="headItems"/>
  <my-table module="role"
            insert-page="/RoleInsert"
            update-page="/RoleUpdate"
            :delete-api="deleteApi"
            :delete-batch-api="deleteBatchApi"
            :delete-callback="page"
            :excel-api="notYet"
            :records="records"
            :columns="columns"
            :buttons="buttons"/>
  <my-pager :page-info="pageInfo"/>
</template>

<style scoped lang="scss">

</style>